package com.surprise.base.threadLocal;

import java.util.HashMap;
import java.util.Map;

/**
 * ThreadLocalContext.java
 * @author xuling
 * @since 2017年1月20日
 */
public class ThreadLocalContext {
	
	/** 保持自己的实例 */
    private static final ThreadLocalContext MY = new ThreadLocalContext();

    /** 线局部键：URL路径 */
	public static final String URLPATH = "URLPATH";
	
    /** 线局部键：Request */
	public static final String REQUEST = "HttpServletRequest";
	
    /** 线局部键：Response */
	public static final String RESPONSE = "HttpServletResponse";
	
    /** 线局部键：OutDTO */
	public static final String OUTDTO = "OutDTO";
	
	/** 线局部键：ApplicationContext */
	public static final String APPLICATIONCONTEXT = "ApplicationContext";
	
	/** 线局部键：ServletActionContext */
	public static final String SERVLETACTIONCONTEXT = "ServletActionContext";
    
	/** 线局部键：Dao */
	public static final String DAO = "Dao";
	
	/** 线局部键：txMg */
	public static final String TX_MG = "txMg";
	
	/** 线局部键：pessimisticLockFlg */
    public static final String PESSIMISTIC_LOCK_FLG = "pessimisticLockFlg"; 
    
    /** 业务开始时间  */
	public static final String TRANTIMESTAMP = "TRANTIMESTAMP";

    /**
     *  线局部键Map
     **/
    private ThreadLocal<Map<String,Object>> context = new ThreadLocal<Map<String,Object>>();

    /**
     * 构造体
     */
    private ThreadLocalContext() {
    }

    /**
     * 线局部键的获得
     *
     * @return 线局部键的返回
     */
    public static final Map<String,Object> getContext() {
        Map<String,Object> ret = null;

        // 线局部键的获得
        ret = MY.context.get();

        if(ret == null) {
            ret = new HashMap<String,Object>();
            MY.context.set(ret);
        }
        return ret;
    }

    /**
     * 线局部键的清理
     */
    public static final void clear() {
        // 线局部键的清理
        MY.context.remove();
    }
}
